define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Joinactivity, $window, $timeout, $ionicSlideBoxDelegate, GoPage, $interval,GoDialog) {

		$scope.selectIndex = 0;
		//切换页面
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectIndex);
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};

		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

		$scope.set_timer = function(now_time) {
			$scope.timer = $interval(function() {
				if($scope.activitys) {
					for(var i = 0; i < $scope.activitys.length; i++) {
						if($scope.activitys[i].now_time != undefined)
							$scope.activitys[i].now_time = $scope.activitys[i].now_time - 1;
						else
							$scope.activitys[i].end_time = $scope.activitys.act[i].end_time - now_time;
						//$scope.activitys[i].now_time = now_time - $scope.activitys.act[i].end_time;
						$scope.activitys[i].formate_time = $scope.formate_time($scope.activitys[i].now_time);
					}
				}
			}, 1000)
		}

		$scope.init_time = function(data, now_time) {
			for(var i = 0; i < data.length; i++) {
				if(data[i].now_time != undefined) {
					data[i].now_time = data[i].now_time - 1;
				} else {
					data[i].now_time = data[i].end_time - now_time;
				}
				data[i].formate_time = $scope.formate_time(data[i].now_time);
			}

		}
		$scope.formate_time = function(time) {
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
				//				console.log(1);
			} else {
				return '0天0小时0分0秒';
			}
		}

		//数据初始化type 1:活动中，2：已结束
		$scope.init_data = function() {
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			var all = window.innerHeight;
			$scope.myObj = {
				"height": (all - lan - 80) + "px"
			}
			Joinactivity.getUnjoinList(1, 1, function(data) {
				$scope.activitys = data.result.activity;
				//console.log(data)
				$scope.init_time($scope.activitys, data.process_time);
				$scope.set_timer(data.process_time);
			});
			Joinactivity.getUnjoinList(2, 1, function(data) {
				$scope.activityslist = data.result.activity;
				//console.log(data)
			});
		}
		$scope.init_data(1);

		//单击按钮事件
		$scope.clickbtn = function(error, act_type, act_id, as_id) {
			if(error) {
				GoDialog.alert("",error);	
			} else {
				//秒杀，团购，限时抢购，商品促销
				if(act_type == "秒杀") {
					GoPage.go_states("seckilldetail", {
						'act_id': act_id
					});
				}
				if(act_type == "团购") {
					GoPage.go_states("groupbuydetail", {
						'act_id': act_id
					});
				}
				if(act_type == "限时抢购") {
					GoPage.go_states("panicbuyingdetail", {
						'act_id': act_id,
					});
				}
				if(act_type == "商品促销") {
					GoPage.go_states("promotionspicture", {
						'act_id': act_id,
						'as_id': as_id
					});
				}
			}
		}
		//页面跳转
		$scope.onshow_first = function(store_id) {
			GoPage.go_states("first", {
				"store_id": store_id
			});
		};

		//下拉刷新
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.input.nPage_end = 1;
		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			if($scope.selectIndex == 0) {
				Joinactivity.getUnjoinList(1, 1, function(data) {
						if(data != null) {
							$scope.activitys = data.result.activity;
							$scope.init_time($scope.activitys, data.process_time);
						} else {
							$scope.activitys = new Object();
						}
						$scope.input.nPage = 1;
					},
					function() {
						$scope.$broadcast('scroll.refreshComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			} else {
				Joinactivity.getUnjoinList(2, 1, function(data) {
						if(data != null) {
							$scope.activityslist = data.result.activity;
							$scope.init_time($scope.activityslist, data.process_time);
						} else {
							$scope.activityslist = new Object();
						}
						$scope.input.nPage_end = 1;
					},
					function() {
						$scope.$broadcast('scroll.refreshComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			}

		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			if($scope.selectIndex == 0) {
				Joinactivity.getUnjoinList(1, $scope.input.nPage + 1, function(data) {
						if(data != null) {
							if($scope.activitys != undefined) {
								$scope.activitys = $scope.activitys.concat(data.result.activity);
								$scope.init_time($scope.activitys, data.process_time);
								$scope.input.nPage += 1;
							} else {
								$scope.activitys = data.result.activity;
								$scope.init_time($scope.activitys, data.process_time);
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});

			} else {
				Joinactivity.getUnjoinList(2, $scope.input.nPage_end + 1, function(data) {
						if(data != null) {
							if($scope.activityslist != undefined) {
								$scope.activityslist = $scope.activityslist.concat(data.result.activity);
								$scope.init_time($scope.activityslist, data.process_time);
								$scope.input.nPage_end += 1;
							} else {
								$scope.activityslist = data.result.activity;
								$scope.init_time($scope.activityslist, data.process_time);
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			}
		};

	}

	controller.$inject = ['$scope', 'Joinactivity', '$window', '$timeout', '$ionicSlideBoxDelegate', 'GoPage', '$interval','GoDialog'];

	return app.registerController("joinactivityctrl", controller);
});